package com.changgou.system.service.impl;

import com.changgou.system.dao.AdminMapper;
import com.changgou.system.pojo.Admin;
import com.changgou.system.service.AdminService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> findAll() {
        return null;
    }

    @Override
    public Admin findById(Integer id) {
        return null;
    }

    @Override
    public void add(Admin admin) {
        //1.获取盐
        String gensalt = BCrypt.gensalt();

        //2.对用户密码加密
        String hashpw = BCrypt.hashpw(admin.getPassword(), gensalt);

        //3.给管理员重新设置密码
        admin.setPassword(hashpw);

        //添加管理员
        adminMapper.insert(admin);
    }

    @Override
    public void update(Admin admin) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public List<Admin> findList(Map<String, Object> searchMap) {
        return null;
    }

    @Override
    public Page<Admin> findPage(int page, int size) {
        return null;
    }

    @Override
    public Page<Admin> findPage(Map<String, Object> searchMap, int page, int size) {
        return null;
    }

    @Override
    public boolean login(Admin admin) {
        //创建一个新的admin对象
        Admin admin1 = new Admin();
        //将登录的用户名赋值给新创建的对象
        admin1.setLoginName(admin.getLoginName());
        //通过新创建的对象来查询数据库中是否存在该管理员账号
        Admin admin2 = adminMapper.selectOne(admin1);
        //对查询数据库返回的对象admin2进行校验
        if (admin2 == null) {
            return false;
        }
        //对密码进行校验
        return BCrypt.checkpw(admin.getPassword(),admin2.getPassword());
    }
}
